import { EventTouch } from 'cc';
import { Vec2 } from 'cc';
import { UITransform } from 'cc';
import { Vec3 } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { EVENT_NAMES } from '../../../../utils/Enums';
import { Layers } from 'cc';
import { Sprite } from 'cc';
import { GGResource } from '../../../../framework/core/base/GGResource';
import Config from '../../../../utils/Config';
import { SpriteFrame } from 'cc';
import { Size } from 'cc';
import DataMgr from '../../../../utils/DataMgr';
import Design from '../../../../utils/Design';
import { AudioManager } from '../../../../common/src/manager/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('DragMove')
export class DragMove extends Component {
    @property({ type: Number, displayName: "编号" })
    index: Number;

    private isDragging: boolean = false;
    private startLoc: Vec2 = new Vec2()
    private touchLoc: Vec2 = new Vec2();

    // 保存的颜色
    private _colors: number[] = []

    start() {
        this.initListener()
        this.startLoc = new Vec2(this.node.position.x, this.node.position.y)

        // gg.systemEvent.emit(EVENT_NAMES.DRAG_RECEIVE_COLOR, this)
    }

    private initListener() {
        // 添加触摸事件监听
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)

        gg.systemEvent.on(EVENT_NAMES.DRAG_OPERATE_RESULT, this.onDragEnd, this)
        gg.systemEvent.on(EVENT_NAMES.PROPERTY_REFRESH, this.onPropertyRefresh, this)
    }

    onDestroy() {
        // 移除触摸事件监听
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);

        gg.systemEvent.off(EVENT_NAMES.DRAG_OPERATE_RESULT, this.onDragEnd, this)
        gg.systemEvent.off(EVENT_NAMES.PROPERTY_REFRESH, this.onPropertyRefresh, this)
    }

    onTouchStart(event: EventTouch) {
        this.isDragging = true;
        this.touchLoc = event.getUILocation()

        AudioManager.playSound("audio:common/拿取音效")
    }

    onTouchMove(event: EventTouch) {
        // // 获取触摸移动的位置
        const touchPos = event.getUILocation();
        const subPos = touchPos.subtract(this.touchLoc)

        const endPos = new Vec2(this.startLoc.x + subPos.x, this.startLoc.y + subPos.y)
        this.node.position = new Vec3(endPos.x, endPos.y, 0)
    }

    onTouchEnd(event: EventTouch) {
        this.isDragging = false
        gg.systemEvent.emit(EVENT_NAMES.DRAG_OPERATE, this.node, event.getLocation(), this.colors)

        AudioManager.playSound("audio:common/放置音效")
    }


    // ------------------- 外部调用 ------------------- //
    // 收获颜色
    public onGameColorsEnd(colors: number[]) {

    }

    // ------------------- 事件 ------------------- //
    private onDragEnd(node: Node, result: boolean) {
        if (node == this.node) {
            this.node.position = new Vec3(this.startLoc.x, this.startLoc.y, 0)
            if (result == true) {
                Config.dragColorArray[parseInt(this.index.toString())] = -1
                gg.systemEvent.emit(EVENT_NAMES.DRAG_RECEIVE_COLOR, this)
            }
        }
    }

    // 刷新
    private onPropertyRefresh() {
        // DataMgr.colorPool.push(this.colors)
        gg.systemEvent.emit(EVENT_NAMES.DRAG_RECEIVE_COLOR, this)
    }

    // ------------------- Set Get ------------------- //
    public set colors(value: number[]) {
        this._colors = value
        this.node.removeAllChildren()

        for (var i = 0; i < value.length; i++) {
            
            const node = new Node()
            node.layer = Layers.Enum.UI_2D
            node.addComponent(Sprite)
            node.position = new Vec3(0, i * 10)
            node.parent = this.node

            const index = i
            GGResource.load("operate:res/textures/" + (value[index] + 1) + "/spriteFrame", SpriteFrame).then((ma: SpriteFrame) => {
                node.getComponent(Sprite).spriteFrame = ma
            });
        }

        if (this._colors.length > 0) {
            Config.dragColorArray[parseInt(this.index.toString())] = this._colors[this._colors.length - 1]
        }
    }

    public get colors(): number[] {
        return this._colors
    }
}


